package com.ruoyi.vqms.service.impl;

import com.ruoyi.vqms.domain.VQMSQueueArea;
import com.ruoyi.vqms.domain.VQMSQueueAreaLocation;
import com.ruoyi.vqms.mapper.VQMSQueueAreaMapper;
import com.ruoyi.vqms.service.IVQMSQueueAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 排队队列 服务层实现
 */
@Service
public class VQMSQueueAreaServiceImpl implements IVQMSQueueAreaService {
    @Autowired
    private VQMSQueueAreaMapper vqmsQueueAreaMapper;

    @Override
    public List<VQMSQueueArea> selectList(VQMSQueueArea vqmsQueueArea) {
        return vqmsQueueAreaMapper.selectList(vqmsQueueArea);
    }

    @Override
    public int add(VQMSQueueArea vqmsQueueArea) {
        int i=vqmsQueueAreaMapper.add(vqmsQueueArea);
        //新增队列库位关系表
        for(Long locationId : vqmsQueueArea.getLocationIds()){
            VQMSQueueAreaLocation areaLocation=new VQMSQueueAreaLocation();
            areaLocation.setAreaId(vqmsQueueArea.getId());
            areaLocation.setLocationId(locationId);
            vqmsQueueAreaMapper.addAreaLocation(areaLocation);
        }
        return i;
    }

    @Override
    public VQMSQueueArea getById(Long id) {
        VQMSQueueArea queueArea=vqmsQueueAreaMapper.getById(id);
        queueArea.setLocationInfo(vqmsQueueAreaMapper.getByQueueAreaId(id));
        return queueArea;
    }

    @Override
    public int update(VQMSQueueArea vqmsQueueArea) {
        //先删除原有队列库位关系表
        vqmsQueueAreaMapper.deleteAreaLocation(new Long[]{vqmsQueueArea.getId()});
        //新增队列库位关系表
        for(Long locationId : vqmsQueueArea.getLocationIds()){
            VQMSQueueAreaLocation areaLocation=new VQMSQueueAreaLocation();
            areaLocation.setAreaId(vqmsQueueArea.getId());
            areaLocation.setLocationId(locationId);
            vqmsQueueAreaMapper.addAreaLocation(areaLocation);
        }
        return vqmsQueueAreaMapper.update(vqmsQueueArea);
    }

    @Override
    public int delete(Long[] ids) {
        //先删除原有队列库位关系表
        vqmsQueueAreaMapper.deleteAreaLocation(ids);
        return vqmsQueueAreaMapper.delete(ids);
    }

    /**
     * 根据队列id查询绑定的库位集合
     * @param areaId
     * @return
     */
    @Override
    public List<Map<String,Object>> getByQueueAreaId(Long areaId){
        return vqmsQueueAreaMapper.getByQueueAreaId(areaId);
    }

    @Override
    public int countByFactoryId(Long factoryId) {
        return vqmsQueueAreaMapper.countByFactoryId(factoryId);
    }

    @Override
    public int countByLocationId(Long locationId) {
        return vqmsQueueAreaMapper.countByLocationId(locationId);
    }
}
